$(document).ready(function() {
	$('#registerForm input').keyup(trimkeyup);

	//Enter可用注册
	$(document).keydown(function(event){
		e = event ? event :(window.event ? window.event : null); 
		if(e.keyCode==13){ 
			next();
		} 
	});
	
	$("#yzmBtn").click(function(){
		delay_till_last('yzmBtn', function() {
			var mobile = $.trim($("#mobile").val());
			if (checkMobile()) {
				$.ajax({
					type: "post",
					url: basePath + "/u/ajaxSendCheckCode",
					cache:false,
					dataType: "text",
					data: {
						"mobile": mobile
					},
					success: function(result){
						if (result == "0") {
							lockGetyzm();
							$("#yzcode").focus();
						} else if (result == "1") {
							$.NotificationApp.send("获取验证码失败","请勿在60秒内重复提交发送请求","top-center","rgba(0,0,0,0.2)","error");
				    	} else if(result == "SEND_TOO_MUCH"){
				    		$.NotificationApp.send("获取验证码失败","今日短信超过限额，请联系客服：0571-82568361","top-center","rgba(0,0,0,0.2)","error");
				    	}else{
				    		$.NotificationApp.send("获取验证码失败","验证码发送失败，请稍候再试","top-center","rgba(0,0,0,0.2)","error");
				    	}
			        }
				});   	
			}
	    }, 300);	
	});
});

//防止重复点击方法
var _timer = {};
function delay_till_last(id, fn, wait) {
    if (_timer[id]) {
        window.clearTimeout(_timer[id]);
        delete _timer[id];
    }
 
    return _timer[id] = window.setTimeout(function() {
        fn();
        delete _timer[id];
    }, wait);
}

function trimkeyup(e) {
     input = $(this);
     if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13) {
         var im = $.trim(input.val());
         input.val(im); 
     }
}

//验证手机号
function checkMobile() {
	var mobile = $.trim($("#mobile").val());
	if (mobile == "") {
		$.NotificationApp.send("获取验证码失败","手机号码不能为空","top-center","rgba(0,0,0,0.2)","error");
		return false;
	}
	
	if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(mobile))){ 
		$.NotificationApp.send("获取验证码失败","手机号码格式不对","top-center","rgba(0,0,0,0.2)","error");
		return false;
    }
	
	if (!ajaxExistMobile(mobile)) {
		return false;
	}
	return true;
}

function ajaxExistMobile(mobile) {
	var notExist = true;

	$.ajax({
		type: "post",
		url: basePath + "/u/ajaxExistMobile",
		async:false,
		cache:false,
		dataType: "text",
		data: {
			"mobile": mobile
		},
		success: function(result){
			if (result == "0") {
				notExist = true;
	    	} else {
	    		$.NotificationApp.send("注册失败","该手机号码已经注册","top-center","rgba(0,0,0,0.2)","error");
	    		notExist = false;
	    	}
        }
	});
	return notExist;
}

function lockGetyzm() {
//	$("#yzmBtn").css("color", "#A7A7A7");
	var count = 60;
	var clock = setInterval(function(){
		if (count > 1) {
			count = count - 1;
			$("#yzmBtn").html(count + "秒后重新获取");
			$('#yzmBtn').attr("disabled",true);
		} else {
			$('#yzmBtn').removeAttr("disabled");
			$("#yzmBtn").html("获取验证码");
			$("#yzmBtn").css("color", "");
			clearInterval(clock);
		}
	}, 1000);
}

